//
//  WithdrawView.m
//  AppleMarket
//
//  Created by yangy on 06/11/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "WithdrawView.h"

// view
#import "BH_UnderLineTextField.h"
// model
#import "CoinAssetModel.h"
#import "AddressModel.h"
// viewModel
#import "WithdrawViewModel.h"

@interface WithdrawView()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) BH_UnderLineTextField *availableTF; //  可提现
@property (nonatomic, strong) BH_UnderLineTextField *addressTF;  // 提现地址
@property (nonatomic, strong) BH_UnderLineTextField *countTF;    // 提现数量
@property (nonatomic, strong) BH_UnderLineTextField *codeTF;     // 验证码
@property (nonatomic, strong) BH_UnderLineTextField *googleTF;
@property (nonatomic, strong) UIButton *commitButton;           // 提交按钮
@property (nonatomic, strong) UILabel *explainLabel;            // 提现说明
@property (nonatomic, strong) WithdrawViewModel *viewModel;
@property (nonatomic, assign) NSInteger time;
@property (nonatomic, strong) RACDisposable *disposable;
@end

@implementation WithdrawView

- (instancetype)initWithFrame:(CGRect)frame viewMode:(id) viewModel
{
    self = [super initWithFrame:frame];
    if(self){
        _viewModel = viewModel;
        
        [self addViews];
        [self bindViewModel];
    }
    return self;
}

- (void) bindViewModel
{
    [self.viewModel.selectAddressSubject subscribeNext:^(AddressModel *x) {
        // 提币地址列表选择了一个地址
        self.addressTF.text = x.BlockAddr;
    }];
    
    RAC(_viewModel,codeStr_) = self.codeTF.rac_textSignal;
    RAC(_viewModel,countStr_) = self.countTF.rac_textSignal;
    RAC(_viewModel,blockAddr_) = self.addressTF.rac_textSignal;
    RAC(_viewModel,googleCodeStr_) = self.googleTF.rac_textSignal;
    
    if([PlistCommon loginModel].IsGoogle){
        
    }
    RAC(_commitButton,enabled) = [RACSignal combineLatest:@[RACObserve(_codeTF, text),RACObserve(_countTF, text),RACObserve(_addressTF, text),RACObserve(_googleTF, text)] reduce:^id (NSString *codeText,NSString *countText,NSString *addrText, NSString *gooleText){
        if(![PlistCommon loginModel].IsGoogle){
            return @(countText.length && countText.length && addrText.length);
        }else{
            return @(countText.length && countText.length && gooleText.length);
        }
    }];
    
    RAC(_commitButton,backgroundColor) = [RACSignal combineLatest:@[RACObserve(_codeTF, text),RACObserve(_countTF, text),RACObserve(_addressTF, text),RACObserve(_googleTF, text)] reduce:^id (NSString *codeText,NSString *countText,NSString *addrText,NSString *googleText){
        if(![PlistCommon loginModel].IsGoogle){
            return (countText.length && countText.length && addrText.length) ? kColorWithHex(0x3291EB) : kColorWithHex(0xADB7C1);
        }else{
            return (countText.length && countText.length && addrText.length && googleText.length) ? kColorWithHex(0x3291EB) : kColorWithHex(0xADB7C1);
        }
    }];
}

- (void) addViews
{
    [self addSubview:self.titleLabel];
    [self addSubview:self.availableTF];
    [self addSubview:self.countTF];
    [self addSubview:self.addressTF];
    [self addSubview:self.codeTF];
    [self addSubview:self.commitButton];
    [self addSubview:self.explainLabel];
    [self setViewsLayout];
}

- (void) setViewsLayout
{
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(30);
        make.top.mas_equalTo(self).offset(29);
    }];
    [_availableTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.right.mas_equalTo(self).offset(-30);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(38);
    }];
    [_addressTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.availableTF.mas_left);
        make.right.mas_equalTo(self.availableTF.mas_right);
        make.top.mas_equalTo(self.availableTF.mas_bottom).offset(18);
    }];
    [_countTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.availableTF.mas_left);
        make.right.mas_equalTo(self.availableTF.mas_right);
        make.top.mas_equalTo(self.addressTF.mas_bottom).offset(18);
    }];
    [_codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.availableTF.mas_left);
        make.right.mas_equalTo(self.availableTF.mas_right);
        make.top.mas_equalTo(self.countTF.mas_bottom).offset(18);
    }];
    
    [_googleTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.availableTF.mas_left);
        make.right.mas_equalTo(self.availableTF.mas_right);
        make.top.mas_equalTo(self.codeTF.mas_bottom).offset(18);
    }];
    
    [_commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.availableTF.mas_left);
        make.right.mas_equalTo(self.availableTF.mas_right);
        make.height.mas_equalTo(40);
        if([PlistCommon loginModel].IsGoogle){
            make.top.mas_equalTo(self.googleTF.mas_bottom).offset(23);
        }else{
            make.top.mas_equalTo(self.codeTF.mas_bottom).offset(23);
        }
    }];
    
    [_explainLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.availableTF.mas_left);
        make.right.mas_equalTo(self.availableTF.mas_right);
        make.top.mas_equalTo(self.commitButton.mas_bottom).offset(90);
    }];
}

- (UILabel *)titleLabel
{
    if(!_titleLabel){
        _titleLabel = [UILabel cm_labelWithText:[NSString stringWithFormat:@"%@提现",self.viewModel.coinModel.Unit] font:kFontBold(26) textColor:kColorWithHex(0x0E304C)];
    }
    return _titleLabel;
}

- (BH_UnderLineTextField *)availableTF
{
    if(!_availableTF){
        _availableTF = [[BH_UnderLineTextField alloc] initWithFrame:CGRectMake(30, 0, k_ScreenWidth - 60, 44)];
        _availableTF.placeholder = [NSString stringWithFormat:@"可提现 %f",self.viewModel.coinModel.Available];
        _availableTF.lineHeight = 1;
        _availableTF.placeHolderColor = kColorWithHex(0x4A647A);
        _availableTF.font = kFont(13);
        _availableTF.rightViewMode = UITextFieldViewModeAlways;
        _availableTF.rightTextFont = kFont(14);
        _availableTF.rightTextColor = kColorWithHex(0x0E304C);
        _availableTF.rightText = self.viewModel.coinModel.Unit;
        _availableTF.enabled = NO;
    }
    return _availableTF;
}

- (BH_UnderLineTextField *)countTF
{
    if(!_countTF){
        _countTF = [[BH_UnderLineTextField alloc] initWithFrame:CGRectMake(30, 0, k_ScreenWidth - 60, 44)];
        _countTF.placeholder = @"提币数量";
        _countTF.lineHeight = 1;
        _countTF.placeHolderColor = kColorWithHex(0x4A647A);
        _countTF.font = kFont(13);
        _countTF.rightViewMode = UITextFieldViewModeAlways;
        _countTF.rightTextFont = kFont(14);
        _countTF.rightTextColor = kColorWithHex(0x0E304C);
        _countTF.errorTextColor = kColorWithHex(0xADB7C1);
        _countTF.rightText = self.viewModel.coinModel.Unit;
        _countTF.error = [NSString stringWithFormat:@"提现手续费率%f%%, 预计到账%f",self.viewModel.coinModel.WithdrawFeesRate * 100,0.0000f];
        [[_codeTF.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
            return value.length;
        }] subscribeNext:^(NSString * _Nullable x) {
            CGFloat rela = [x floatValue] * (1 - self.viewModel.coinModel.WithdrawFeesRate);
            NSString *errorText = [NSString stringWithFormat:@"提现手续费率%f%%, 预计到账%f%@",self.viewModel.coinModel.WithdrawFeesRate * 100,rela <= 0? 0.00 : rela,self.viewModel.coinModel.Unit];
            self.codeTF.error = errorText;
        }];
        _codeTF.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _countTF;
}

- (BH_UnderLineTextField *)addressTF
{
    if(!_addressTF){
        _addressTF = [[BH_UnderLineTextField alloc] initWithFrame:CGRectMake(30, 0, k_ScreenWidth - 60, 44)];
        _addressTF.font = kFont(13);
        _addressTF.lineHeight = 1;
        _addressTF.placeholder = @"提币地址";
        _addressTF.placeHolderColor = kColorWithHex(0x4A647A);
        _addressTF.rightViewMode = UITextFieldViewModeAlways;
        _addressTF.rightImage = [UIImage imageNamed:@"more"];
        @weakify(self);
        [_addressTF setRightHanlder:^{
            @strongify(self);
            [self.viewModel.addressClickSubject sendNext:nil];
        }];
    }
    return _addressTF;
}

- (BH_UnderLineTextField *)codeTF
{
    if(!_codeTF){
        _codeTF = [[BH_UnderLineTextField alloc] initWithFrame:CGRectMake(30, 0, k_ScreenWidth - 60, 44)];
        _codeTF.font = kFont(13);
        _codeTF.lineHeight = 1;
        _codeTF.placeholder = @"验证码";
        _codeTF.rightViewMode = UITextFieldViewModeAlways;
        _codeTF.rightTextFont = kFont(14);
        _codeTF.rightTextColor = kColorWithHex(0x3291EB);
        _codeTF.rightText = @"获取验证码";
        _codeTF.placeHolderColor = kColorWithHex(0x4A647A);
        _codeTF.keyboardType = UIKeyboardTypeNumberPad;
        @weakify(self);
        _codeTF.rightHanlder = ^{
            @strongify(self);
            UIButton *sendButton = (UIButton*)self.codeTF.rightView;
            sendButton.enabled = NO;
            self.time = 60;
            self.disposable = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {
                self.time -- ;
                NSString *title = self.time > 0 ? [NSString stringWithFormat:@"%lds",self.time] : @"重新发送";
                UIColor *bgColor =  self.time > 0 ? kColorWithHex(0xADB7C1) : kColorWithHex(0x3291EB);
                [sendButton setTitle:title forState:UIControlStateNormal | UIControlStateDisabled];
                [sendButton setTitleColor:bgColor forState:UIControlStateNormal | UIControlStateDisabled];
                sendButton.enabled = (self.time <= 0) ? YES : NO;
                if(self.time == 0){
                    [self.disposable dispose];
                }
            }];
            [self.viewModel.sendCodeClickSubject sendNext:nil];
        };
    }
    return _codeTF;
}

- (BH_UnderLineTextField *)googleTF
{
    if(!_googleTF){
        _googleTF = [[BH_UnderLineTextField alloc] initWithFrame:CGRectMake(30, 0, k_ScreenWidth - 60, 44)];
        _googleTF.font = kFont(13);
        _codeTF.lineHeight = 1;
        _googleTF.placeholder = @"谷歌验证码";
        _googleTF.rightViewMode = UITextFieldViewModeAlways;
        _googleTF.placeHolderColor = kColorWithHex(0x4A647A);
        _googleTF.keyboardType = UIKeyboardTypeNumberPad;
        _googleTF.hidden = ![PlistCommon loginModel].IsGoogle;
    }
    return _codeTF;
}

- (UIButton *)commitButton
{
    if(!_commitButton){
        _commitButton = [UIButton cm_ButtonWithTitle:@"提交" font:kFont(14) titleColor:[UIColor whiteColor] bgColor:kColorWithHex(0xADB7C1)];
        _commitButton.layer.cornerRadius = 2;
        [[_commitButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self.viewModel.commitClickSubject sendNext:nil];
        }];
    }
    return _commitButton;
}

- (UILabel *)explainLabel
{
    if(!_explainLabel){
        _explainLabel = [UILabel cm_labelWithText:@"" font:kFont(12) textColor:kColorWithHex(0xADB7C1)];
        _explainLabel.lineBreakMode = NSLineBreakByCharWrapping;
        _explainLabel.numberOfLines = 0;
        _explainLabel.attributedText = [[NSAttributedString alloc] initWithData:[self.viewModel.coinModel.WithdrawTips dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    }
    return _explainLabel;
}


@end
